Method Overriding

Scala overriding method provides your own implementation of it. When a class inherits from another, it may want to modify the definition for a method of the superclass or provide a new version of it. This is the concept of Scala method overriding and we use the ‘override’ modifier to implement this.


Here, in the diagram stated above School is the super-class which has a method defined in it which is named as NumberOfStudents() and this method is overridden by the sub-classes i.e, class 1, class 2, class 3. so, all the sub-classes has the same named method as defined in the super-class.

When to apply Method Overriding ?

when a subclass wishes to impart a particular implementation for the method defined in the parent class then that subclass overrides the defined method from the parent class. When we wish to reconstruct the method defined in the super class then we can apply method overriding.


// Scala program of method overriding

// Creating a super class
class School
{
// Method defined
def NumberOfStudents()=
{
0 // Utilized for returning an Integer
}
}

// Creating a subclass
class class_1 extends School
{
// Using Override keyword
override def NumberOfStudents()=
{
30
}
}

// Creating a subclass
class class_2 extends School
{
// Using override keyword
override def NumberOfStudents()=
{
32
}
}

// Creating a subclass
class class_3 extends School
{
// Using override keyword
override def NumberOfStudents()=
{
29
}
}

// Creating object
object Demo
{
// Main method
def main(args:Array[String])
{
// Creating instances of all
// the sub-classes
var x=new class_1()
var y=new class_2()
var z=new class_3()

// Displays number of students in class_1
println("Number of students in class 1 : " + x.NumberOfStudents())

// Displays number of students in class_2
println("Number of students in class 2 : " + y.NumberOfStudents())

// Displays number of students in class_3
println("Number of students in class 3 : " + z.NumberOfStudents())

}
}



// Scala program of method overriding

// Creating a super class
class Shapes
{
// Method defined with parameters
def Area(l:Double, b:Double, r:Double)=
{
0.0 // Utilized for returning double
}
}

// Creating a subclass
class Rectangle extends Shapes
{
// Overriding method to find
// area of the rectangle
override def Area(l:Double, b:Double, r:Double)=
{
(l * b)
}
}

// Creating a subclass
class Circle extends Shapes
{
// Overriding method to find
// area of the circle
override def Area(l:Double, b:Double, r:Double)=
{
((3.14)* r * r)
}
}

// Creating object
object Demo
{
// Main method
def main(args:Array[String])
{
// Creating instances of all
// the sub-classes
var rectangle = new Rectangle()
var circle = new Circle()

// Displays area of the rectangle
println(rectangle.Area(3, 11, 4))

// Displays area of the circle
println(circle.Area(1, 7, 10))
}
}

Rules for Method Overriding

There are a few restrictions that we need to follow for method overriding, these are as follows:

  • For method overriding, one of the crucial rule is that the class which is overriding needs to utilize the modifier override or override annotation.
  • Auxiliary constructors are not able to call the super-class constructors immediately. They can hardly call the primary constructors which in reversal will call the super-class constructor.
  • In Method Overriding, we won’t be able to override a var with a def or val, otherwise it throws an error.
  • Here, we won’t be able to override a val in the super-class by a var or def in the subclass, and if the var in the super-class is abstract then we can override it in the subclass.
  • If a field is stated var then it can override the def which is defined in the super-class. The var can only override a getter or setter combination in the super-class.



No comments:

Post a Comment